package leetcode.leetcode_111;

import java.util.LinkedList;

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> nodeList = new LinkedList<>();
        nodeList.add(root);
        int  ans  = 0 ;
        while(!nodeList.isEmpty()){
            ans ++;
            int size = nodeList.size();
            for (int i =0 ;i<size ;i++){
                TreeNode poll = nodeList.poll();
                if (poll.left == null && poll.right == null) {
                    return ans;
                }
                if (poll.left != null) {
                    nodeList.add(poll.left);
                }
                if (poll.right != null) {
                    nodeList.add(poll.right);
                }
            }
        }
        return ans;
    }
}